package com.ocom.xfrecode.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfRecordOrg;
import com.ocom.common.request.exposeapi.LargeScreenRequest;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.StringUtils;
import com.ocom.xfrecode.mapper.XfRecordOrgMapper;
import com.ocom.xfrecode.service.XfRecordOrgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class XfRecordServiceOrgImpl extends ServiceImpl<XfRecordOrgMapper, EntityXfRecordOrg> implements XfRecordOrgService {

    @Autowired
    private XfRecordOrgMapper xfRecordMapper;


    @Override
    public Page<EntityXfRecordOrg> webSelectDetails(JSONObject jsonObject) {

        QueryWrapper<EntityXfRecordOrg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id",jsonObject.getLong("comId"));
        if (jsonObject.getLong("merchId")!=null && jsonObject.getLong("merchId")!=-1){
            queryWrapper.eq("merch_id",jsonObject.getLong("merchId"));
        }
        if (jsonObject.getLong("pointId")!=null && jsonObject.getLong("pointId")!=-1){
            queryWrapper.eq("point_id",jsonObject.getLong("pointId"));
        }
        if (jsonObject.getString("deviceNo")!=null){
            queryWrapper.like("device_no",jsonObject.getString("deviceNo"));
        }
        if (jsonObject.getLong("pId")!=null){
            queryWrapper.eq("p_id",jsonObject.getLong("pId"));
        }
        if (jsonObject.getString("pName")!=null){
            queryWrapper.like("p_name",jsonObject.getString("pName"));
        }
        if (jsonObject.getString("accountNo")!=null){
            queryWrapper.like("account_no",jsonObject.getString("accountNo"));
        }
        if (jsonObject.getString("channelOrderNo")!=null){
            queryWrapper.like("channel_order_no",jsonObject.getString("channelOrderNo"));
        }
        if (jsonObject.getString("orderNo")!=null){
            queryWrapper.like("order_no",jsonObject.getString("orderNo"));
        }
        if (jsonObject.getString("pCode")!=null){
            queryWrapper.like("p_code",jsonObject.getString("pCode"));
        }
        if (jsonObject.getString("cardNo")!=null){
            queryWrapper.like("card_no",jsonObject.getString("cardNo"));
        }
        if (jsonObject.getInteger("consState")!=null){
            queryWrapper.eq("cons_state",jsonObject.getInteger("consState"));
        }
        if (jsonObject.getInteger("deptId")!=null){
            queryWrapper.eq("dept_id",jsonObject.getInteger("deptId"));
        }
        queryWrapper.lt("pay_state",100);
        queryWrapper.orderByDesc("cons_time");
        //消费
        if (StringUtils.isNotEmpty(jsonObject.getString("startTime")) && StringUtils.isNotEmpty(jsonObject.getString("endTime"))){
            String start = jsonObject.getString("startTime").substring(0, 7).replace("-", "");
            String end = jsonObject.getString("endTime").substring(0, 7).replace("-", "");
            List<String> dates = new ArrayList<>();
            try {
                dates = DateUtils.getMonthBetween(start, end);
            } catch (Exception e) {
                log.info("请求方式:【{}】,查询的请求的数据:【{}】,异常信息:【{}】","获取消费记录信息",jsonObject,e);
            }
            //小于等于
            queryWrapper.le("cons_time", DateUtils.strToDate(jsonObject.getString("endTime")));
            //大于等于
            queryWrapper.ge("cons_time", DateUtils.strToDate(jsonObject.getString("startTime")));

            List<String> finalDates = dates;
            finalDates.add(DateUtils.getMaxMonthDate(end));
            //日期范围查询
            queryWrapper.and(wrapper -> {
                for (String info : finalDates) {
                    wrapper.or().eq("month_value", info);
                }
            });
        }else {
            String end = DateUtils.getStrDate(new Date(),"yyyy-MM-dd HH:mm:ss").substring(0, 7).replace("-", "");
            queryWrapper.eq("month_value",end);
        }
        Page<EntityXfRecordOrg> page = new Page<>(jsonObject.getInteger("page"), jsonObject.getInteger("size"));
        return xfRecordMapper.selectPage(page,queryWrapper);
    }

    @Override
    public List<EntityXfRecordOrg> yesterdayStudentMealRate(LargeScreenRequest request) {

        String day  = request.getParam().getString("day");
        if(day ==null || day.length()!=10){
            return  null;
        }

        Integer accTypeId  = request.getParam().getInteger("accTypeId");

        String monthValue =day.substring(0,7).replace("-","");

        QueryWrapper<EntityXfRecordOrg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id",Long.valueOf(request.getCompanyId()));
        queryWrapper.eq("meal_date",day);
        queryWrapper.eq("if_refund",0);
        queryWrapper.eq("pay_state",1);

        if(accTypeId !=null && accTypeId>0 ){
            queryWrapper.eq("acc_type_id",accTypeId);
        }

        queryWrapper.select("meal_no","count(distinct p_id) as consState","sum(0-cons_money) as consType");

        queryWrapper.groupBy("meal_no");
        queryWrapper.orderByAsc("meal_no");
        queryWrapper.eq("month_value",monthValue);

        return   xfRecordMapper.selectList(queryWrapper);
    }


}
